// Copyright Epic Games, Inc. All Rights Reserved.

#include "SmartMenuStyle.h"

#include "Framework/Application/SlateApplication.h"
#include "Styling/SlateStyle.h"
#include "Styling/SlateStyleRegistry.h"
#include "Interfaces/IPluginManager.h"

//TSharedPtr< FSlateStyleSet > FSmartMenuStyle::SmartStyleInstance = NULL;
TUniquePtr< FSlateStyleSet > FSmartMenuStyle::SmartStyleInstance = nullptr;

void FSmartMenuStyle::Initialize()
{
	if (!SmartStyleInstance.IsValid())
	{
		SmartStyleInstance = Create();
		FSlateStyleRegistry::RegisterSlateStyle(*SmartStyleInstance);
	}
}

void FSmartMenuStyle::Shutdown()
{
	if (SmartStyleInstance.IsValid())
	{
		FSlateStyleRegistry::UnRegisterSlateStyle(*SmartStyleInstance);
		SmartStyleInstance.Reset();
	}
}

FName FSmartMenuStyle::GetStyleSetName()
{
	static FName StyleSetName(TEXT("SmartStyle"));
	return StyleSetName;
}

FName FSmartMenuStyle::GetContextName()
{
	static FName ContextName(TEXT("Smart"));
	return ContextName;
}

#define IMAGE_BRUSH( RelativePath, ... ) FSlateImageBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )

const FVector2D Icon20x20(20.0f, 20.0f);
const FVector2D Icon40x40(40.0f, 40.0f);

TUniquePtr< FSlateStyleSet > FSmartMenuStyle::Create()
{
	TUniquePtr< FSlateStyleSet > Style = MakeUnique<FSlateStyleSet>(GetStyleSetName());
	// Style->SetContentRoot(FPaths::EnginePluginsDir() / TEXT("Experimental/Smart/Resources"));
	//Style->SetContentRoot(FPaths:: / TEXT("Experimental/Smart/Resources"));

	//return Style;

	//TSharedRef< FSlateStyleSet > Style = MakeShareable(new FSlateStyleSet(GetStyleSetName()));
	Style->SetContentRoot(IPluginManager::Get().FindPlugin("SmartStudio")->GetBaseDir() / TEXT("Resources"));

	//Style->Set("SmartToolbar.PluginAction", new IMAGE_BRUSH(TEXT("ButtonIcon_40x"), Icon40x40));

	return Style;

}

void FSmartMenuStyle::SetIcon(const FString& StyleName, const FString& ResourcePath)
{
	FSlateStyleSet* Style = SmartStyleInstance.Get();

	FString Name(GetContextName().ToString());
	Name = Name + "." + StyleName;
	Style->Set(*Name, new IMAGE_BRUSH(ResourcePath, Icon40x40));

	Name += ".Small";
	Style->Set(*Name, new IMAGE_BRUSH(ResourcePath, Icon20x20));

	FSlateApplication::Get().GetRenderer()->ReloadTextureResources();
}

#undef IMAGE_BRUSH

const ISlateStyle& FSmartMenuStyle::Get()
{
	check(SmartStyleInstance);
	return *SmartStyleInstance;
}
